﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SecurityInformation.maHoa
{
    class Ceasar
    {
        private String test = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        private String plainTex;
        private String cipherText;
        public String PlainText
        {
            get { return this.plainTex; }
            set { this.plainTex = value; }
        }
        public String CipherText
        {
            get { return this.cipherText; }
            set { this.cipherText = value; }
        }
        //hàm mã hóa Caesar
        public String encryption(String phainText, int key)
        {
            this.plainTex = phainText.ToUpper();
            this.cipherText = null;
            for (int i = 0; i < this.plainTex.Length; i++)
                for(int j=0;j<this.test.Length;j++)
                    if (this.plainTex[i] == test[j])
                    {
                        this.cipherText += test[(j + key) % 26];
                        break;
                    }
                    else if (this.plainTex[i] == ' ')
                    {
                        this.cipherText += ' ';
                        break;
                    }

            return this.cipherText;
        }
        //hàm giải mã Ceasar
        public String decryption(String cipherText, int key)
        {
            this.cipherText = cipherText.ToUpper();
            this.plainTex = null;
            for(int i=0;i<this.cipherText.Length;i++)
                for(int j=0;j<this.test.Length;j++)
                    if (this.cipherText[i] == test[j])
                    {
                        int t = j - key>0 ? j - key : j - key + 26;
                        this.plainTex += test[t % 26];
                        break;
                    }
                    else if (this.cipherText[i] == ' ')
                    {
                        this.plainTex += ' ';
                        break;
                    }

            return this.plainTex;
        }
    }
}
